package com.anis.project.medcare.search.dialog;

import java.util.List;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.PartInitException;

import com.anis.jpa.medical.model.Patient;
import com.anis.project.medcare.Activator;
import com.anis.project.medcare.editors.PatientEditor;
import com.anis.project.medcare.editors.input.PatientEditorInput;
import com.anis.project.medcare.search.table.providers.SearchPatientContentProv;
import com.anis.project.medcare.search.table.providers.SearchPatientLabelProv;

public class SearchPatientDialog extends Dialog {
	private Text patientNameText;
	private Text patientPhooneText;
	private Text patientLstNameText;
	private TableViewer table;
	protected boolean retrieveAll;
	private Patient selectedPatient;
	private Text patientIDText;

	/**
	 * Create the dialog.
	 * 
	 * @param parentShell
	 */
	public SearchPatientDialog(Shell parentShell) {
		super(parentShell);
	}

	/**
	 * Create contents of the dialog.
	 * 
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		container.setLayout(new GridLayout(4, false));

		final Button btnRetrieveAll = new Button(container, SWT.CHECK);
		btnRetrieveAll.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (btnRetrieveAll.getSelection()) {
					patientNameText.setEnabled(false);
					patientLstNameText.setEnabled(false);
					patientPhooneText.setEnabled(false);
					patientIDText.setEnabled(false);
					retrieveAll = true;
				} else {
					patientNameText.setEnabled(true);
					patientLstNameText.setEnabled(true);
					patientPhooneText.setEnabled(true);
					patientIDText.setEnabled(true);
					retrieveAll = false;
				}
			}
		});
		btnRetrieveAll.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false,
				false, 1, 1));
		btnRetrieveAll.setText("Retrieve All");
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);

		Label lblPatientName = new Label(container, SWT.NONE);
		lblPatientName.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false,
				false, 1, 1));
		lblPatientName.setText("Patient Name :");

		patientNameText = new Text(container, SWT.BORDER);
		patientNameText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
				false, 3, 1));

		Label lblPatientLastName = new Label(container, SWT.NONE);
		lblPatientLastName.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
				false, false, 1, 1));
		lblPatientLastName.setText("Patient Last Name :");

		patientLstNameText = new Text(container, SWT.BORDER);
		patientLstNameText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
				false, 3, 1));

		Label lblPhone = new Label(container, SWT.NONE);
		lblPhone.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false,
				1, 1));
		lblPhone.setText("Phone :");

		patientPhooneText = new Text(container, SWT.BORDER);
		GridData gd_patientPhooneText = new GridData(SWT.FILL, SWT.CENTER,
				true, false, 1, 1);
		gd_patientPhooneText.widthHint = 125;
		patientPhooneText.setLayoutData(gd_patientPhooneText);

		Label lblPatientId = new Label(container, SWT.NONE);
		lblPatientId.setLayoutData(new GridData(SWT.RIGHT, SWT.FILL, false,
				false, 1, 1));
		lblPatientId.setText("Patient ID :");

		patientIDText = new Text(container, SWT.BORDER);
		patientIDText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);

		Button btnNewButton = new Button(container, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				retrieveInputOfTable(retrieveAll, table);
			}
		});
		btnNewButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false,
				false, 1, 1));
		btnNewButton.setText("Look up");

		table = new TableViewer(container, SWT.BORDER | SWT.FULL_SELECTION);
		final Table table_1 = table.getTable();
		table_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				TableItem selectedItem = table_1.getSelection()[0];
				if (selectedItem.getData() instanceof Patient) {
					selectedPatient = (Patient) selectedItem.getData();

				}
			}
		});
		table_1.setLinesVisible(true);
		table_1.setHeaderVisible(true);
		table_1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, true, 4,
				1));

		TableViewerColumn tableViewerColumn = new TableViewerColumn(table,
				SWT.NONE);
		TableColumn tblclmnPatientId = tableViewerColumn.getColumn();
		tblclmnPatientId.setWidth(100);
		tblclmnPatientId.setText("Patient ID");

		TableViewerColumn tableViewerColumn_1 = new TableViewerColumn(table,
				SWT.NONE);
		TableColumn tblclmnPatientName = tableViewerColumn_1.getColumn();
		tblclmnPatientName.setWidth(109);
		tblclmnPatientName.setText("Patient Name");

		TableViewerColumn tableViewerColumn_2 = new TableViewerColumn(table,
				SWT.NONE);
		TableColumn tblclmnPatientLastName = tableViewerColumn_2.getColumn();
		tblclmnPatientLastName.setWidth(122);
		tblclmnPatientLastName.setText("Patient Last Name");

		TableViewerColumn tableViewerColumn_3 = new TableViewerColumn(table,
				SWT.NONE);
		TableColumn tblclmnPatientPhone = tableViewerColumn_3.getColumn();
		tblclmnPatientPhone.setWidth(100);
		tblclmnPatientPhone.setText("Patient Phone");

		return container;
	}

	/**
	 * Create contents of the button bar.
	 * 
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,
				true);
		createButton(parent, IDialogConstants.CANCEL_ID,
				IDialogConstants.CANCEL_LABEL, false);
	}

	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(546, 360);
	}

	private void retrieveInputOfTable(boolean retrieveAll, TableViewer table) {
		List<Patient> patientList;
		if (!retrieveAll) {
			String patientName = patientNameText.getText();
			String patientLastName = patientLstNameText.getText();
			Integer patientPhone = null;
			Integer patientID = null;
			if (!patientPhooneText.getText().isEmpty())
				patientPhone = Integer.valueOf(patientPhooneText.getText());
			if (!patientIDText.getText().isEmpty())
				patientID = Integer.valueOf(patientIDText.getText());

			if (patientNameText.getText().isEmpty())
				patientName = null;
			if (patientLstNameText.getText().isEmpty())
				patientLastName = null;

			patientList = Activator
					.getDefault()
					.databaseBinder()
					.retrievePatient(patientName, patientLastName,
							patientPhone, patientID);

		} else {
			patientList = Activator.getDefault().databaseBinder()
					.retrieveAllPatient();
		}
		table.setLabelProvider(new SearchPatientLabelProv());
		table.setContentProvider(new SearchPatientContentProv());
		table.setInput(patientList);
	}

	@Override
	protected void okPressed() {
		super.okPressed();
		PatientEditorInput patientEditorInput = new PatientEditorInput(
				selectedPatient);
		try {
			PatientEditor patientEditor = (PatientEditor) Activator
					.getDefault().getWorkbench().getActiveWorkbenchWindow()
					.getActivePage()
					.openEditor(patientEditorInput, PatientEditor.ID);
		} catch (PartInitException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

	}
}
